/**
 * created by 小卷毛, 2022-02-28
 * Copyright (c) 2022, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import android.graphics.Color
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.extend.CustomValueFormatter
import com.lzg.extend.BaseResponse
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.rzico.sbl.R
import com.rzico.sbl.model.*
import com.rzico.sbl.other.ColorBarDataSet
import com.rzico.sbl.ui.report.other.ReportUrl
import com.xinnuo.common.extend.getColorEx
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally

/**
 * 描 述：报货总经理水站操作相关
 * 作 者：小卷毛  2022-02-28 09:19
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class ReportStationViewModel : ColorViewModel() {

    /**
     * 设置销量 BarChart 数据
     */
    fun initSaleBarData(
        barChart: BarChart,
        entries: List<BarEntry>
    ) {
        barChart.apply {
            if (data != null && data.dataSetCount > 0) {
                (data.getDataSetByIndex(0) as BarDataSet).entries = entries
                data.notifyDataChanged()
                notifyDataSetChanged()
            } else {
                data = BarData(
                    ColorBarDataSet(color2(), entries, "").apply {
                        colors = color2()                  //设置柱子颜色，循环使用
                        highLightColor = Color.TRANSPARENT //高亮颜色
                        isHighlightEnabled = false         //选中柱子是否高亮显示
                    }
                ).apply {
                    barWidth = 0.4f
                    setValueTextSize(10f)
                    setValueTextColor(context.getColorEx(R.color.black))
                    setValueFormatter(CustomValueFormatter())
                }
            }
        }
    }

    /**
     * 水站列表
     */
    fun stationList(
        page: Int,
        beginDate: String,
        endDate: String,
        onSuccess: (ArrayList<ManagerStation>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<ManagerStation>>>(ReportUrl.getBaseUrl().zStaList)
            .params(
                getParams(
                    "beginDate" to beginDate,
                    "endDate" to endDate,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<ManagerStation>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 销量、金额柱状图数据
     */
    fun amountList(
        memberId: String,
        beginDate: String,
        endDate: String,
        onSuccess: (ArrayList<ManagerDate>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<ManagerDate>>>(ReportUrl.getBaseUrl().zStaSale)
            .params(
                getParams(
                    "memberId" to memberId,
                    "beginDate" to beginDate,
                    "endDate" to endDate
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<ManagerDate>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 品牌柱状图数据
     */
    fun goodsBrandList(
        memberId: String,
        beginDate: String,
        endDate: String,
        onSuccess: (ArrayList<ManagerGoodsData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<ManagerGoodsData>>>(ReportUrl.getBaseUrl().zGoodsTwo)
            .params(
                getParams(
                    "memberId" to memberId,
                    "beginDate" to beginDate,
                    "endDate" to endDate
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<ManagerGoodsData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 商品柱状图数据
     */
    fun goodsSaleList(
        memberId: String,
        goodsSn: String,
        beginDate: String,
        endDate: String,
        onSuccess: (ArrayList<ManagerDate>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<ManagerDate>>>(ReportUrl.getBaseUrl().zGoodsTwo2)
            .params(
                getParams(
                    "sn" to goodsSn,
                    "memberId" to memberId,
                    "beginDate" to beginDate,
                    "endDate" to endDate
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<ManagerDate>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 水站分布列表
     */
    fun stationLayoutList() =
        OkGo.get<BaseResponse<ArrayList<MasterData>>>(ReportUrl.getBaseUrl().rptStaLoca)
            .converter(object : JacksonConvert<BaseResponse<ArrayList<MasterData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }
            .map { it.data }

}